package com.emovimento.synchronize;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

/**
 * SynchronizeFile represents a <code>java.io.File</code> to be used for synchronization. The only difference is the equals method which
 * makes SynchronizeFiles equals that reside in different directories.
 */
public class SynchronizeFile extends File {
  private static final long serialVersionUID = 1L;

  public SynchronizeFile(String pathname) {
    super(pathname);
  }

  public SynchronizeFile(File file) {
    super(file.getAbsolutePath());
  }

  public SynchronizeFile(File parent, String child) {
    super(parent, child);
  }

  /**
   * Tests this abstract pathname for equality with the given object. Returns <code>true</code> if and only if the argument is not
   * <code>null</code> and is an abstract pathname that denotes a file or directory with the same filename (but not necessarily the same
   * abstract pathname).
   * 
   * @param obj The object to be compared with this abstract pathname
   * @return <code>true</code> if and only if the objects are the same; <code>false</code> otherwise
   */
  @Override
  public boolean equals(Object obj) {
    if ((obj != null) && (obj instanceof SynchronizeFile)) { return this.getName().equals(((File) obj).getName()); }
    return false;
  }

  /**
   * @see java.io.File#listFiles()
   */
  public Collection<SynchronizeFile> getSynchronizeFiles() {
    Collection<SynchronizeFile> synchronizeFiles = new ArrayList<>();

    for (File currentFile : super.listFiles()) {
      synchronizeFiles.add(new SynchronizeFile(currentFile));
    }

    return synchronizeFiles;
  }

}
